//
//  XGSquareCollectionViewCell.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/15.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <Masonry/Masonry.h>
#import "UIImageView+Extension.h"

#import "XGSquareCollectionViewCell.h"

#import "XGSquareModel.h"

@interface XGSquareCollectionViewCell ()

/// 图标
@property (nonatomic,strong) UIImageView *iconImageView;
/// 标题
@property (nonatomic,strong) UILabel *titleLabel;

@end

@implementation XGSquareCollectionViewCell

#pragma mark - 解析模型

- (void)setSquareModel:(XGSquareModel *)squareModel
{
    _squareModel = squareModel;
    
    if (squareModel.icon != nil) {
        [self.iconImageView xg_setImageWithURLString:squareModel.icon placeHolder:kPlaceHolderImage];
    }
    
    if (squareModel.name != nil) {
        self.titleLabel.text = squareModel.name;
    }
}

#pragma mark - 构造方法

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI
{
    self.backgroundColor = [UIColor whiteColor];
    // 添加子控件
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.titleLabel];
    
    // 设置自动布局
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(kMargin);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.centerX.equalTo(self.contentView);
    }];
}

#pragma mark - 懒加载

- (UIImageView *)iconImageView
{
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.layer.cornerRadius = 60 / 2;
        _iconImageView.layer.masksToBounds = YES;
    }
    
    return _iconImageView;
}

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel labelWithText:@"" textColor:[UIColor lightGrayColor] font:12 textAlignment:NSTextAlignmentCenter];
    }
    
    return _titleLabel;
}

@end
